//
//  LoginViewController.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/24.
//

#import "LoginViewController.h"
#import "YMCountDown.h"
#import "CountDownBtn.h"
#import "LoginTextField.h"
#import "TTTAttributedLabel.h"
#import "AreaChooseViewController.h"
#import "WebViewController.h"
#import "LoginManager.h"
#import "MineUserModel.h"
#import "SendSmsCodeManager.h"
#import "AreaListModel.h"
#import <WXApi.h>
#import "BindPhoneViewController.h"
#import "NSString+NumberVerify.h"

@interface LoginViewController ()<UITextFieldDelegate,YMCountDownDelegate,NoDoubleButtonDelegate,TTTAttributedLabelDelegate,JPAPIManagerCallBackDelegate>

@property(nonatomic,strong)LoginTextField *phoneTF;
@property(nonatomic,strong)LoginTextField *codeTF;
@property(nonatomic,strong)CountDownBtn *countDownBtn;
@property(nonatomic,strong)YMCountDown *countDown;
@property(nonatomic,strong)UIButton *selBtn;
@property(nonatomic,strong)UIButton *areaBtn;
@property(nonatomic,strong)LoginManager *loginManager;
@property(nonatomic,strong)MineUserModel *userModel;
@property(nonatomic,strong)SendSmsCodeManager *sendCodeManager;
@property(nonatomic,strong)AreaListModel *areaModel;
@property(nonatomic,strong)JPProgressHUD *hub;
@property(nonatomic,strong)TTTAttributedLabel *rulesLabel;
@property(nonatomic,strong)UIImageView *bgImageView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIButton *loginBtn;

@end

@implementation LoginViewController

- (LoginManager *)loginManager{
    if(!_loginManager){
        _loginManager = [[LoginManager alloc]init];
        _loginManager.delegate = self;
    }
    return _loginManager;
}

-(SendSmsCodeManager *)sendCodeManager{
    if(!_sendCodeManager){
        _sendCodeManager = [[SendSmsCodeManager alloc]init];
        _sendCodeManager.delegate = self;
    }
    return _sendCodeManager;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [self createUI];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(wxLoginSuccess:) name:@"WXLoginSuccess" object:nil];
    
}

#pragma mark - 微信授权成功 登录
-(void)wxLoginSuccess:(NSNotification*)notifi{
    NSString *code = notifi.object;
    if(code){
        //登录
        self.hub = [JPProgressHUD showMsg:@"微信授权成功,正在登录..."];
        self.loginManager.loginMode = @"3";
        self.loginManager.wxCode = code;
        [self.loginManager loadData];
    }
}

#pragma mark - 绑定手机
-(void)bindPhone{
    BindPhoneViewController *vc = [[BindPhoneViewController alloc]init];
    vc.type = 1;
    vc.userModel = self.userModel;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)createUI{
    
    self.customNavigationBar.backgroundColor = [UIColor clearColor];
    CGFloat w282 = [DeviceUtils getAdaptedWidthForDesignWidth:282 andCurrentAvailableWidth:self.currentAvailableWidth];
    if(w282 > self.currentAvailableHeight){
        w282 = self.currentAvailableHeight;
    }
    UIImageView *bgImageView = [[UIImageView alloc]init];
    bgImageView.image = [UIImage imageNamed:@"mine_login_topbg"];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(@0);
        make.height.mas_equalTo(w282);
    }];
    self.bgImageView = bgImageView;
    
//    UIButton *guestLoginBtn = [[UIButton alloc]init];
//    [guestLoginBtn setTitle:@"游客登录" forState:UIControlStateNormal];
//    [guestLoginBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
//    guestLoginBtn.titleLabel.font = JPUIFontPingFangSCMedium(15);
//    [guestLoginBtn addTarget:self action:@selector(guestLoginBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.customNavigationBar addSubview:guestLoginBtn];
//    CGFloat g_w = [self getStringWidthWithText:guestLoginBtn.titleLabel.text font:guestLoginBtn.titleLabel.font viewHeight:40];
//    [guestLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(@-11);
//        make.height.mas_equalTo(@40);
//        make.bottom.mas_equalTo(@-2);
//        make.width.mas_equalTo(g_w+10);
//    }];
    
    CGFloat w166 = [DeviceUtils getAdaptedWidthForDesignWidth:166 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat max_top = self.currentAvailableHeight-14-39-84-52-14-52-70-kWFit(49)-70;
    if(isIPAD){
        max_top = self.currentAvailableHeight-14-39-84-52-14-52-70-49-70;
    }
    if(w166 > max_top){
        w166 = max_top;
    }
    __weak typeof(self)self_weak_  = self;
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(28);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"欢迎登录";
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@39);
        make.top.mas_equalTo(w166+14);
        make.centerX.mas_equalTo(self_weak_.view);
    }];
    self.titleLabel = titleLabel;
    
    self.areaModel = [[AreaListModel alloc]init];
    self.areaModel._id = @"1";
    self.areaModel.areaId = @"86";
    self.areaModel.areaName = @"中国(China)";
    
    self.phoneTF = [[LoginTextField alloc]init];
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneTF placeHolderStr:@"请输入你的手机号"];
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,70,52)];
    CGFloat a_w = [self getStringWidthWithText:@"+86" font:JPUIFontInterMedium(15) viewHeight:46];
    self.areaBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,3,a_w+16,46)];
    [self.areaBtn setTitle:@"+86" forState:UIControlStateNormal];
    [self.areaBtn setTitleColor:KFontBlackColor forState:UIControlStateNormal];
    self.areaBtn.titleLabel.font = JPUIFontInterMedium(15);
    [self.areaBtn addTarget:self action:@selector(areaBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:self.areaBtn];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(53.5,20,0.5,12)];
    line.backgroundColor = UIColorHex(0xE5E5E5);
    [leftView addSubview:line];
    self.phoneTF.leftView = leftView;
    self.phoneTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@24);
        make.right.mas_equalTo(@-24);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(84);
        make.height.mas_equalTo(@52);
    }];
    
    self.codeTF = [[LoginTextField alloc]init];
    self.codeTF.keyboardType = UIKeyboardTypeDefault;
    [self.codeTF placeHolderStr:@"请输入验证码"];
    CGFloat c_w = [self getStringWidthWithText:@"获取验证码" font:JPUIFontPingFangSCRegular(13) viewHeight:46];
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, c_w+16,52)];
    self.countDownBtn = [[CountDownBtn alloc]initWithFrame:CGRectMake(2,3,c_w+14,46)];
    [self.countDownBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.countDownBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    self.countDownBtn.titleLabel.font = JPUIFontPingFangSCRegular(13);
    self.countDownBtn.delegate = self;
    [self.countDownBtn addTarget:self action:@selector(countDownClick) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:self.countDownBtn];
    self.codeTF.rightView = rightView;
    self.codeTF.rightViewMode = UITextFieldViewModeAlways;
    self.codeTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,8,52)];
    self.codeTF.leftViewMode = UITextFieldViewModeAlways;
    self.codeTF.delegate = self;
    [self.view addSubview:self.codeTF];
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@24);
        make.right.mas_equalTo(@-24);
        make.top.mas_equalTo(self_weak_.phoneTF.mas_bottom).offset(14);
        make.height.mas_equalTo(@52);
    }];
    if (!self.countDown) {
        self.countDown = [[YMCountDown alloc] init];
        self.countDown.delegate = self;
    }
    
    self.selBtn = [[UIButton alloc]init];
    [self.selBtn setImage:[UIImage imageNamed:@"mine_login_tick_unsel"] forState:UIControlStateNormal];
    [self.selBtn setImage:[UIImage imageNamed:@"mine_login_tick_sel"] forState:UIControlStateSelected];
    self.selBtn.selected = YES;
    self.selBtn.adjustsImageWhenHighlighted = NO;
    [self.selBtn addTarget:self action:@selector(selClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.selBtn];
    [self.selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@24);
        make.left.mas_equalTo(@27);
        make.top.mas_equalTo(self_weak_.codeTF.mas_bottom).offset(10);
    }];
    
    NSString *linkText = @"《平台服务协议》";
    NSString *linkText2 = @"《隐私政策》";
    NSString *promptText = [NSString stringWithFormat:@"我已阅读并同意%@与%@", linkText, linkText2];
    NSRange linkRange = [promptText rangeOfString:linkText];
    NSRange linkRange2 = [promptText rangeOfString:linkText2];
    CGFloat labelWidth = self.currentAvailableWidth - 51 - 24;
    TTTAttributedLabel *rulesLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(51, w166 + 265, labelWidth, 0)];
    rulesLabel.font = JPUIFontPingFangSCRegular(13);
    rulesLabel.textColor = KFontGrayColor;
    rulesLabel.delegate = self;
    rulesLabel.numberOfLines = 0; // 允许多行显示
    rulesLabel.lineBreakMode = NSLineBreakByWordWrapping; // 按单词换行
    rulesLabel.preferredMaxLayoutWidth = labelWidth; // 设置最大宽度以支持自动换行
    NSDictionary *attributesDic = @{(NSString *)kCTForegroundColorAttributeName: (__bridge id)KFontBlackColor.CGColor,
                                    (NSString *)kCTUnderlineStyleAttributeName : @(NO)};
    rulesLabel.linkAttributes = attributesDic;
    rulesLabel.activeLinkAttributes = attributesDic;
    rulesLabel.text = promptText;
    [rulesLabel addLinkToURL:[NSURL URLWithString:@"protocol"] withRange:linkRange];
    [rulesLabel addLinkToURL:[NSURL URLWithString:@"policy"] withRange:linkRange2];
    CGSize size = [rulesLabel sizeThatFits:CGSizeMake(labelWidth, CGFLOAT_MAX)];
    CGRect labelFrame = rulesLabel.frame;
    if(size.height < 24){
        size.height = 24;
    }
    labelFrame.size.height = size.height;
    rulesLabel.frame = labelFrame;
    [self.view addSubview:rulesLabel];

    self.rulesLabel = rulesLabel;
    
    UIButton *loginBtn = [[UIButton alloc]init];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = JPUIFontPingFangSCMedium(15);
    if(isIPAD){
        loginBtn.layer.cornerRadius = 49/2.0;
    }else{
        loginBtn.layer.cornerRadius = kWFit(49)/2.0;
    }
    loginBtn.layer.masksToBounds = YES;
    loginBtn.backgroundColor = KThemeColor;
    loginBtn.ym_acceptEventInterval = 2.0;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@24);
        if(isIPAD){
            make.height.mas_equalTo(@49);
        }else{
            make.height.mas_equalTo(kWFit(49));
        }
        make.right.mas_equalTo(@-24);
        make.top.mas_equalTo(self_weak_.codeTF.mas_bottom).offset(70);
    }];
    self.loginBtn = loginBtn;
    
    
//    if([WXApi isWXAppInstalled] == YES){
//        
//        UIButton *wechatBtn = [[UIButton alloc]init];
//        [wechatBtn setImage:[UIImage imageNamed:@"mine_login_wechat"] forState:UIControlStateNormal];
//        [wechatBtn addTarget:self action:@selector(wechatBtnClick) forControlEvents:UIControlEventTouchUpInside];
//        wechatBtn.ym_acceptEventInterval = 2.0;
//        [self.view addSubview:wechatBtn];
//        [wechatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.mas_equalTo(@-68);
//            make.width.height.mas_equalTo(@48);
//            make.centerX.mas_equalTo(self_weak_.view);
//        }];
//        
//        UILabel *tipLabel = [[UILabel alloc]init];
//        tipLabel.font = JPUIFontPingFangSCRegular(13);
//        tipLabel.textColor = KFontBlackColor;
//        tipLabel.textAlignment = NSTextAlignmentCenter;
//        tipLabel.text = @"其他方式登录";
//        [self.view addSubview:tipLabel];
//        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.mas_equalTo(wechatBtn.mas_top).offset(-24);
//            make.width.mas_equalTo(kScreenWidth-48);
//            make.centerX.mas_equalTo(self_weak_.view);
//        }];
//        
//    }
}

#pragma mark - 屏幕宽度改变
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if(isIPAD){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 获取当前可用宽度并更新布局
            self.currentAvailableWidth = [DeviceUtils currentAvailableWidth];
            self.currentAvailableHeight = [DeviceUtils currentAvailableHeight];
            self.customNavigationBar.frame = CGRectMake(0, 0, self.currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT);
            CGFloat w282 = [DeviceUtils getAdaptedWidthForDesignWidth:282 andCurrentAvailableWidth:self.currentAvailableWidth];
            if(w282 > self.currentAvailableHeight){
                w282 = self.currentAvailableHeight;
            }
            [self.bgImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(w282);
            }];
            CGFloat w166 = [DeviceUtils getAdaptedWidthForDesignWidth:166 andCurrentAvailableWidth:self.currentAvailableWidth];
            CGFloat max_top = self.currentAvailableHeight-14-39-84-52-14-52-70-49-70;
            if(w166 > max_top){
                w166 = max_top;
            }
            [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(w166+14);
            }];
            __weak typeof(self)self_weak_  = self;
            [self.phoneTF mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self_weak_.titleLabel.mas_bottom).offset(84);
            }];
            [self.codeTF mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self_weak_.phoneTF.mas_bottom).offset(14);
            }];
            [self.selBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self_weak_.codeTF.mas_bottom).offset(10);
            }];
            CGFloat labelWidth = self.currentAvailableWidth - 51 - 24;
            CGSize size = [self.rulesLabel sizeThatFits:CGSizeMake(labelWidth, CGFLOAT_MAX)];
            if(size.height < 24){
                size.height = 24;
            }
            self.rulesLabel.frame = CGRectMake(51, w166 + 265, labelWidth,size.height);
            [self.loginBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self_weak_.codeTF.mas_bottom).offset(70);
            }];
            
        });
    }
}

#pragma mark - 微信登录
-(void)wechatBtnClick{
    if([WXApi isWXAppInstalled] == YES){
        [self sendAuthRequest];
    }
}

#pragma mark - 微信授权
-(void)sendAuthRequest{
    SendAuthReq* req = [[SendAuthReq alloc]init];
    req.scope = @"snsapi_userinfo"; // 只能填 snsapi_userinfo 获取用户信息
    req.state = @"jpIsland";
    [WXApi sendReq:req completion:^(BOOL success) {
        if(success == NO){
            dispatch_async(dispatch_get_main_queue(), ^{
                [JPProgressHUD showMsg:@"授权失败"];
            });
        }
    }];
}


#pragma mark - 登录
-(void)loginBtnClick{
    if(self.selBtn.selected == NO){
        [JPProgressHUD showMsg:@"请先同意协议"];
        return;
    }
    if(self.phoneTF.text.length == 0){
        [JPProgressHUD showMsg:@"请输入手机号"];
        return;
    }
    
    BOOL isValid = [self.phoneTF.text parseForPhoneNumberWithCountryCode:self.areaModel.areaId];
    if(!isValid){
        [JPProgressHUD showMsg:@"手机号格式不正确"];
        return;
    }
    
    if(self.codeTF.text.length == 0){
        [JPProgressHUD showMsg:@"请输入验证码"];
        return;
    }
    
    
    self.loginManager.loginMode = @"2";
    self.loginManager.phone = [NSString stringWithFormat:@"%@%@",self.areaModel.areaId,self.phoneTF.text];
    self.loginManager.mobileCode = self.codeTF.text;
    [self.loginManager loadData];
    
}

#pragma mark - 区号选择
-(void)areaBtnClick{
    AreaChooseViewController *vc = [[AreaChooseViewController alloc]init];
    vc.selAreaModel = self.areaModel;
    [self.navigationController pushViewController:vc animated:YES];
    __weak typeof(self)self_weak_ = self;
    [vc setSelBlock:^(AreaListModel * _Nonnull areaModel) {
        self_weak_.areaModel = areaModel;
        [self_weak_.areaBtn setTitle:[NSString stringWithFormat:@"+%@",areaModel.areaId] forState:UIControlStateNormal];
    }];
}

#pragma mark - 同意协议
-(void)selClick{
    self.selBtn.selected = !self.selBtn.selected;
}

#pragma mark - 协议点击
-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
    WebViewController *vc = [[WebViewController alloc]init];
    if ([url.absoluteString isEqualToString:@"protocol"]) {
        //协议
        vc.webUrlString = @"https://www.riyudao.com/guanwang/app/use.html";
        vc.titleStr = @"平台服务协议";
    }else if([url.absoluteString isEqualToString:@"policy"]) {
       //政策
       vc.webUrlString = @"https://www.riyudao.com/guanwang/app/hide.html";
       vc.titleStr = @"隐私政策";
    }
    [self.navigationController pushViewController:vc animated:YES];
}

//#pragma mark - 游客登录
//-(void)guestLoginBtnClick{
//    
//    self.loginManager.loginMode = @"4";
//    [self.loginManager loadData];
//    
//}


#pragma mark - 获取验证码
-(void)countDownClick{
    
    if(self.phoneTF.text.length == 0){
        [JPProgressHUD showMsg:@"请输入手机号"];
        return;
    }
    
    BOOL isValid = [self.phoneTF.text parseForPhoneNumberWithCountryCode:self.areaModel.areaId];
    if(isValid){
        self.sendCodeManager.mobile = [NSString stringWithFormat:@"%@%@",self.areaModel.areaId,self.phoneTF.text];
        [self.sendCodeManager loadData];
    }else{
        [JPProgressHUD showMsg:@"手机号格式不正确"];
    }
    
}

-(void)BtnClick:(UIButton *)btn{
    [self countDownClick];
}

#pragma mark - 倒计时代理 JPCountryCodeChoiceControllerDelegate
- (void)countDownWillFire:(YMCountDown *)countDown{
    self.countDownBtn.userInteractionEnabled = NO;
}

-(void)countDown:(YMCountDown *)countDown runningTime:(NSTimeInterval)runningTime{
    NSString *str = [NSString stringWithFormat:@"%is",(int)runningTime];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.countDownBtn setTitle:str forState:UIControlStateNormal];
    }];

}

-(void)countDown:(YMCountDown *)countDown didStopInTime:(NSTimeInterval)time{
     self.countDownBtn.userInteractionEnabled = YES;
    [self.countDownBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
}

#pragma mark - JPAPIManagerCallBackDelegate
-(void)managerCallDLDLAPIDidSuccess:(JPAPIBaseManager *)manager{
    if([manager isEqual:self.loginManager]){
        if(self.hub){
            [self.hub removeFromSuperview];
        }
        NSDictionary *dic = manager.response.content;
        if(dic[@"message"]){
            [JPProgressHUD showMsg:dic[@"message"]];
        }
//        [[NSUserDefaults standardUserDefaults]setObject:self.loginManager.loginMode forKey:@"loginMode"];
        self.userModel = [MineUserModel mj_objectWithKeyValues:dic[@"data"][@"user"]];
        if(self.userModel.phone){
            if(self.userModel.userId){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.userId forKey:@"userId"];
            }
            if(self.userModel.phone){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.phone forKey:@"phone"];
            }
            if(self.userModel.nickname){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.nickname forKey:@"nickname"];
            }
            if(self.userModel.avatar){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.avatar forKey:@"avatar"];
            }
            if(self.userModel.sex){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.sex forKey:@"sex"];
            }
            if(self.userModel.introduction){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.introduction forKey:@"introduction"];
            }
            if(self.userModel.goldNumber){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.goldNumber forKey:@"goldNumber"];
            }
            
            if(self.userModel.signCount){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.signCount forKey:@"signCount"];
            }
            
            if(self.userModel.unionid){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.unionid forKey:@"unionid"];
            }
            
            if(dic[@"data"][@"access_token"]){
                [[NSUserDefaults standardUserDefaults]setObject:dic[@"data"][@"access_token"] forKey:@"access_token"];
            }
            [[NSNotificationCenter defaultCenter]postNotificationName:@"loginSuccess" object:self.userModel];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            if(self.userModel.unionid){
                [[NSUserDefaults standardUserDefaults]setObject:self.userModel.unionid forKey:@"unionid"];
            }
            //如果没绑定手机号
            [self bindPhone];
        }
    }
    
    
    if([manager isEqual:self.sendCodeManager]){
        NSDictionary *dic = manager.response.content;
        if(dic[@"message"]){
            [JPProgressHUD showMsg:dic[@"message"]];
        }
        [self.countDown fire];
    }
}

-(void)managerCallDLDLAPIDidFailed:(JPAPIBaseManager *)manager{
    if(self.hub){
        [self.hub removeFromSuperview];
    }
    if(manager.errorMessage){
        [JPProgressHUD showMsg:manager.errorMessage];
    }
}


@end
